/**
 * Created on 18 nov. 2011 at 14:43:34 by HozakaN
 */
package fr.castorhoz.isi_connectivity_widget.widget_configuration.custom_adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;
import fr.castorhoz.isi_connectivity_widget.widget_configuration.WidgetConfiguration;
import fr.castorhoz.isi_connectivity_widget.widget_configuration.WidgetConfigurationLineClickedListener;


/**
 * @author jlimbert
 *
 */
public class WidgetListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<ConnButton.widgets> widgetList;
	private WidgetConfiguration wConf;
	
	public WidgetListAdapter(WidgetConfiguration wConf, Context context, List<ConnButton.widgets> widgetList) {
		this.widgetList = widgetList;
		inflater = LayoutInflater.from(context);
		this.wConf = wConf;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return widgetList.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		return widgetList.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		// Pour chaque cellule du tableau une view est cree et affichee
		// Cette vue sera fondee sur un layout speifique
		// On peut creer un layout pour chaque cellule de la listView
		
		WidgetLineHolder holder;
				
		if (convertView == null)
		{
				//convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_2, null);
				convertView = inflater.inflate(R.layout.uc_list_widget_configuration_item, null);
				WidgetConfigurationLineClickedListener clickListener = new WidgetConfigurationLineClickedListener();
				clickListener.addObserver(wConf);
				convertView.setOnClickListener(clickListener);
				holder = new WidgetLineHolder();
				holder.textView = (TextView)convertView.findViewById(R.id.ucListItemText);
				holder.checkbox = (CheckBox)convertView.findViewById(R.id.includeWidgetCheckBox);
				/*holder.secondTV = (TextView)convertView.findViewById(R.id.phoneTextBox);
				holder.ib = (ImageButton)convertView.findViewById(R.id.openDirectory);
				holder.ib.setImageResource(android.R.drawable.ic_delete);
				holder.firstTV.setText(items.get(position));*/
				//holder.ib.setTag(new OpenDirectoryCtrl(holder.et, holder.ib));
				/*holder.ib.setOnClickListener(new DeletePhoneNumberLineCtrl(this));*/
				convertView.setTag(holder);
		}
		else
		{
			holder = (WidgetLineHolder)convertView.getTag();
		}
		
		holder.textView.setText(widgetList.get(position).name());
		/*String text = holder.et.getText().toString();
		if (!text.equals(items.get(position))) {
			items.remove(position);
			items.add(position, text);
		}*/
		return convertView;
	}

}
